#version 460
#extension GL_GOOGLE_include_directive : enable

#include "../../../../core/shader_binding_ID.h"
#include "../../../../core/shader_taster_transform.h"
#include "../../../../core/shader_taster_color.h"



layout(triangles) in;
layout(triangle_strip, max_vertices=32) out;



layout(std430, column_major, binding = 3) readonly buffer U3_ {
	float m_Scale[];
};

layout(std140, set=0, binding=4) uniform U4_ {
	int isAloneScale;
	float m_ZScale;
	float m_ZOffset;
	float m_z;
};

//in gl_PerVertex {
//	vec4	gl_Position;
//	float	gl_PointSize;
//} gl_in[];

layout(location = 1) in vec3 inPos[];
layout(location = 2) in uint inInstance[];



out gl_PerVertex {
	vec4	gl_Position;
};
layout(location = 0) out vec4 outColor;
layout(location = 1) out vec3 outNormal;
layout(location = 2) out vec3 outViewLine;
layout(location = 3) out vec2 Zso;

//vec3 vs[3] = {vec3(-100,-500,0), vec3(-100,500,0), vec3(500,00,0)};
void main(void) {
	
	vec3 edgeA = normalize(inPos[1] - inPos[0]);
	vec3 edgeB = normalize(inPos[2] - inPos[0]);
	vec3 Normal = cross(edgeA, edgeB);

	vec4 color = f_glsl_buildColor(InstanceColor[inInstance[0]]);
	color = vec4(1);

	float s = 1.0;
	if (isAloneScale != 0) {
		s = m_Scale[inInstance[0]];
	}

	mat4 mat = mat_Model[inInstance[0]];
	for(int i=0; i<3; ++i) {
		
		gl_Position = mat_ProjecView[0] * mat * vec4(inPos[i] * s, 1);
		//gl_Position = gl_in[i].gl_Position;
		mat3x3 normalMat = mat3x3(mat);
		outNormal = normalize((normalMat * Normal).xyz);

		outColor = color;
		
		outViewLine = (inverse(mat3(mat_ProjecView[1])) * vec3(0,0,1));
		Zso = vec2(m_ZScale, m_ZOffset);
		EmitVertex();
	}

	EndPrimitive();
}


